package tq.com.tqteacher.message.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.systools.system.StringHelper;
import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.base.BaseTitleActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.message.model.TQNoticeModel;
import tq.com.tqteacher.net.TQNetInterface;

public class TQNewNoticeActivity extends BaseTitleActivity {
    private TQNewNoticeFragment mNoticeFragment = new TQNewNoticeFragment();
    @Override
    protected BaseFragment fragment() {
        return mNoticeFragment;
    }

    @Override
    protected void initHeader(TQHeader5View pHeader5View) {
        super.initHeader(pHeader5View);
        pHeader5View.getTitleView().setText("新建公告");
        pHeader5View.getRightView().setText("完成");

        pHeader5View.getRightView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mNoticeFragment.commit();
            }
        });
    }

    static public class TQNewNoticeFragment extends BaseFragment{
        private EditText mEditText;
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View lView = inflater.inflate(R.layout.fragment_teacherinfo_layout, container, false);
            mEditText = lView.findViewById(R.id.content);
            mEditText.setHint("输入公告");
            return lView;
        }

        public void commit(){
            final String content = StringHelper.editText(mEditText);
            if(StringHelper.isEmpty(content)){
                AlertDialogHelper.show(getContext(), "公告内容不能为空");
                return;
            }

            TQNetInterface.newNotice(content, new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQNewNoticeFragment.this.getContext(), pNetResult.getErrMsg());
                        return;
                    }

                    if(pNetResult.getData() == null)return;

                    TQNoticeModel lTQNoticeModel = new TQNoticeModel();
                    lTQNoticeModel.setContent(content);
                    lTQNoticeModel.onResult(pNetResult.getData());

                    TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.add, lTQNoticeModel));
                    getActivity().finish();
                }
            });


        }
    }
}
